package com.xbmzdx.fstj_app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbmzdx.fstj_app.service.WeatherService;
import io.swagger.annotations.Example;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WeatherServiceImpl implements WeatherService {

    private final static Logger logger = LoggerFactory.getLogger(Example.class);

    public String findWeather(String jing,String wei){
        String html = "https://weather.com/zh-CN/weather/today/l/"+jing+","+wei;
        String html1 = getHtml (html);
        return getZWX (html1);
    }

    public static String getHtml(String url){
        String html = "";
        try {
            html = Jsoup.connect(url).execute().body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }
    public static String getZWX(String html){
        List list = new ArrayList ();
        String s =null;
        Document document = Jsoup.parse(html);
        Elements elements = document.select("div [class=WeatherDetailsListItem--wxData--2s6HT]").select ("span");
        int len = elements.size();
        for (int i = 0; i < len; i++) {
            list.add(elements.get(i));
        }
        s = list.get (6).toString ();
        String zwx = s.replace ("<span data-testid=\"UVIndexValue\">","").replace ("</span>","");
        logger.info("当前紫外线强度为"+zwx);
        return "当前紫外线强度为"+zwx;
    }

    public  Map testGetParameter(String address) throws Exception {
        return testGetParameter (address,null);
    }

    public  Map testGetParameter(String address,String city) throws Exception{
        Map map = new HashMap();
        //1.创建httpclient对象
        CloseableHttpClient client=HttpClients.createDefault();
        //2.创建封装的URI对象，可以存放参数
        URIBuilder uri=new URIBuilder("https://api.map.baidu.com/geocoding/v3/");
        //存放参数--可以参照地址栏：键值对方法
        uri.addParameter("address",address);
        uri.addParameter("city",city);
        uri.addParameter("ak","NRPbQTpXN5N5LGHTbQHqLio11vMW6FOY");
        uri.addParameter("output","json");
        //3.创建get对象:并放入URI
        HttpGet get=new HttpGet(uri.build());
        //4.执行get方法，并返回响应结果
        CloseableHttpResponse response = client.execute(get);
        //5.处理结果
        //1.获取结果状态码
        int statusCode = response.getStatusLine().getStatusCode();
        //2.获取结果实体内容
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity, "utf-8");
        JSONObject objects = (JSONObject) JSONObject.parse (content);
        JSONObject result = (JSONObject) objects.get ("result");
        JSONObject location = (JSONObject) result.get ("location");
        String lng = location.getString ("lng");
        String lat = location.getString ("lat");
        map.put ("lng",lng);
        map.put ("lat",lat);
        //6.关闭连接
        client.close();
        return map;
    }

}
